package net.lzzy.chapter13;

public class  GenericSort {
    public static void main(String[] args) {
        Integer[] intArray = {2,4,3};
        Double[] doublesArray = {1.1,2.2,-3.3};
        Character[] charArray = {'W','G','L'};
        String[] stringsArray = {"韦","古","龙"};
        Student s1 = new Student(1,"张三","男",111.2);
        Student s2 = new Student(2,"李四","男",100.3);
        Student s3 = new Student(3,"王五","男",134.4);

        Student[] stu = new Student[]{s2,s1,s3};
        sort(stu);

        sort(intArray);
        sort(doublesArray);
        sort(charArray);
        sort(stringsArray);

        System.out.print("Sorted Integer objects: ");
        printList(intArray);
        System.out.print("Sorted Double objects: ");
        printList(doublesArray);
        System.out.print("Sorted Character objects: ");
        printList(charArray);
        System.out.print("Sorted String objects: ");
        printList(stringsArray);
        printList(stu);

    }

    public static <E extends  Comparable<E>> void sort(E[] list){
        E currentMin;
        int currentMinIndex;

        for(int i = 0 ; i< list.length - 1 ; i++){
            currentMin = list[i];
            currentMinIndex = i;

            for (int j = 0 ; j< list.length - 1 ; j++){
                if(currentMin.compareTo(list[j]) > 0){
                    currentMin = list[j];
                    currentMinIndex = j;
                }
            }

            if (currentMinIndex !=i){
                list[currentMinIndex] = list[i];
                list[i] = currentMin;
            }
        }
    }

    public static void printList(Object[] list){
        for (int i = 0 ; i < list.length ; i++){
            System.out.print(list[i] + " ");
        }
        System.out.println();
    }
}
